Functions & Variable Lifecycle — Code Cards (Classroom Version)

Try me

Open In ColabBinder

How to use

  • Each card mirrors an A4 classroom prompt. Predict first (or discuss), then run the cell to check.

  • Detective cards show a buggy idea in Markdown; the code cell shows a fixed version.

  • Keep explanations short and schematic (whatwhy).

Turn Gemini into a coding tutor (no direct answers)

Paste this in your first chat with Gemini to keep it in “tutor mode”:

You are a coding tutor for Python in Jupyter/Colab. Follow the course motto “do not give learning.”
Role: Use Socratic guidance and test‑first thinking so I solve problems myself.

Rules
1) Do NOT provide full working solutions. Show at most tiny fragments (≤3 lines) or TODO-style pseudocode.
2) Prefer questions over answers; offer one small next step at a time.
3) When debugging, read the traceback, give 2–3 hypotheses, propose the smallest change in plain English.
4) Encourage TDD: ask me to write an assert, predict, run, and report results.
5) Keep replies concise (≈120–150 words) unless I ask for deep dives.
6) Ask me to run code and share output; adapt based on results.
7) If I request the full solution, remind me of the rules and offer a higher‑tier hint instead.

Key Concepts

Card KEY-1 — Code Wizard (Answer the question)

Is x = len("abc") an expression or a statement? Why?
(Discuss; then run to inspect the value.)
[ ]:

# KEY-1 — inspect x = len("abc")

Card KEY-2 — Code Wizard (Answer the question)

Is y = abs(-8) a function call, a variable assignment statement, or both? Explain in your own words.
(Discuss; then run to inspect the value.)
[ ]:

# KEY-2 — inspect y = abs(-8)

Function Definition & Calls

Card FUN-1 — Code Wizard (Fix the code)

Fix the code to define a variable x, assign "Hasta la vista baby", and print

x = print("Hasta la vista baby")
[ ]:

# FUN-1 — Fix! x = print("Hasta la vista baby")

Card FUN-2 — Code Wizard (Fix the code)

Define a polynomial function f(x) = x^2 + 2x + 1 that returns the value. Then call f(3).

# FUN-2 — Fix!
def f(x):
    y = x**2 + 1
print(f(3)) ## Expected 16
[ ]:

# FUN-2 — Fix! def f(x): y = x**2 + 1 print(f(3)) ## Expected 16

Card FUN-3 — Predict the output (default used vs overridden)

def greet(name, punctuation="!"):
    return f"Hello, {name}{punctuation}"

print(greet("Ada"))
print(greet("Ada", "?"))
print(greet(name="Grace"))

Question: What three lines are printed? Why?

[ ]:

# FUN-3 — run to check def greet(name, punctuation="!"): return f"Hello, {name}{punctuation}" print(greet("Ada")) # default "!" used print(greet("Ada", "?")) # override default print(greet(name="Grace")) # keyword uses default

Card FUN-4 — Predict the Output (positional & keyword)

def power(base, exp=2):
    return base ** exp

print(power(3))
print(power(2, 3))
print(power(exp=3, base=2))

Question: Predict the three numbers and explain which call used the default.

[ ]:
# DEF-2 — run to check
def power(base, exp=2):
    return base ** exp

print(power(3))
print(power(2, 3))
print(power(exp=3, base=2))

Card FUN-5 — Code Detective (mutable default trap)

Buggy idea:

def add_item(x, bag=[]):
    bag.append(x)
    return bag

Task: Explain the bug and fix so repeated calls don’t share state. (Hint: WHat happens if bag is None)

[ ]:

def add_item(x, bag=[]): bag.append(x) return bag print(add_item("a")) print(add_item("b")) print(add_item("c", None))

Card FUN-6 — Mini‑Design: totals with defaults (tax & discount)

Complete the code below to complete function total(price, qty=1, tax=0.21, discount=0.0) that returns the final price after applying discount first, then tax: final = price * qty * (1 - discount) * (1 + tax)

Calls to predict:

print(total(10))
print(total(10, qty=3))
print(total(10, tax=0.10, discount=0.20))
print(total(price=10, discount=0.10, qty=2, tax=0.21))
[ ]:

# FUN-6 — reference implementation & checks def total(price, qty=1, tax=0.21, discount=0.0): """Return final cost after discount then tax.""" print(total(10)) # 12.1 print(total(10, qty=3)) # 36.3 print(total(10, tax=0.10, discount=0.20)) # 8.8 print(total(price=10, discount=0.10, qty=2, tax=0.21)) # 21.78

Card FUN7 — Code Wizard (Return vs Print)

Consider:

def area(w, h):
    print(w * h)
a = area(3, 4)

Question: What is stored in a? Refactor to return the value and print at the call site.

[ ]:

def area(w, h): #TODO: Fix to return the area a = area(3, 4) print("Area:", a) # Expected: Area: 12

Card FUN-7 — Code Wizard Challenge (write & call with mixed arguments)

Write rectangle(w, h=1, unit="cm") -> str that returns a formatted string like "3x4 cm".
Then call it as:
print(rectangle(3, 4)) # Expected 3x4 cm
print(rectangle(5))    # Expected 5x1 cm
print(rectangle(h=2, w=7, unit="mm")) # Expected 2x7 cm
[ ]:

# DEF-7 — reference solution def rectangle(w, h=1, unit="cm"): #TODO: Complete print(rectangle(3, 4)) # 3x4 cm print(rectangle(5)) # 5x1 cm print(rectangle(h=2, w=7, unit="mm")) # 7x2 mm